#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 1000
#define MAX_COURSES 6
#define FILENAME "students.txt"

typedef struct {
    char id[20];          // 学号
    char name[50];        // 姓名
    char class[20];       // 班级
    int scores[MAX_COURSES]; // 多门课程的成绩
} Student;

Student students[MAX_STUDENTS];
int student_count = 0;

// 从文件加载学生信息
void load_students() {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        printf("文件未找到或无法打开。\n");
        return;
    }

    while (fscanf(file, "%s %s %s %d %d %d %d %d %d", 
                  students[student_count].id, 
                  students[student_count].name, 
                  students[student_count].class, 
                  &students[student_count].scores[0], 
                  &students[student_count].scores[1], 
                  &students[student_count].scores[2], 
                  &students[student_count].scores[3], 
                  &students[student_count].scores[4], 
                  &students[student_count].scores[5]) != EOF) {
        student_count++;
    }

    fclose(file);
}

// 保存学生信息到文件
void save_students() {
    FILE *file = fopen(FILENAME, "w");
    if (file == NULL) {
        printf("保存文件时出错！\n");
        return;
    }

    for (int i = 0; i < student_count; i++) {
        fprintf(file, "%s %s %s %d %d %d %d %d %d\n", 
                students[i].id, 
                students[i].name, 
                students[i].class, 
                students[i].scores[0], 
                students[i].scores[1], 
                students[i].scores[2], 
                students[i].scores[3], 
                students[i].scores[4], 
                students[i].scores[5]);
    }

    fclose(file);
}

// 添加学生信息
void add_student() {
    if (student_count >= MAX_STUDENTS) {
        printf("无法添加更多学生，人数已达上限。\n");
        return;
    }

    Student new_student;
    printf("请输入学号: ");
    scanf("%s", new_student.id);
    printf("请输入姓名: ");
    scanf("%s", new_student.name);
    printf("请输入班级: ");
    scanf("%s", new_student.class);
    printf("请输入6门课程的成绩（用空格分隔）: ");
    for (int i = 0; i < MAX_COURSES; i++) {
        scanf("%d", &new_student.scores[i]);
    }

    students[student_count++] = new_student;
    save_students();
    printf("学生信息添加成功。\n");
}

// 删除学生信息
void delete_student() {
    char id[20];
    printf("请输入要删除的学生的学号: ");
    scanf("%s", id);

    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            for (int j = i; j < student_count - 1; j++) {
                students[j] = students[j + 1];
            }
            student_count--;
            save_students();
            printf("学生信息删除成功。\n");
            return;
        }
    }

    printf("未找到该学生。\n");
}

// 修改学生信息
void modify_student() {
    char id[20];
    printf("请输入要修改的学生的学号: ");
    scanf("%s", id);

    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            printf("请输入新的姓名: ");
            scanf("%s", students[i].name);
            printf("请输入新的班级: ");
            scanf("%s", students[i].class);
            printf("请输入新的6门课程的成绩（用空格分隔）: ");
            for (int j = 0; j < MAX_COURSES; j++) {
                scanf("%d", &students[i].scores[j]);
            }

            save_students();
            printf("学生信息修改成功。\n");
            return;
        }
    }

    printf("未找到该学生。\n");
}

// 查询学生人数
void query_student_count() {
    printf("学生总人数: %d\n", student_count);
}

// 按班级查询学生人数
void query_by_class() {
    char class[20];
    printf("请输入要查询的班级: ");
    scanf("%s", class);

    int count = 0;
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].class, class) == 0) {
            count++;
        }
    }

    printf("班级 %s 的学生人数: %d\n", class, count);
}

// 计算学生的平均分数
void calculate_average_score() {
    int total = 0;
    for (int i = 0; i < student_count; i++) {
        for (int j = 0; j < MAX_COURSES; j++) {
            total += students[i].scores[j];
        }
    }

    printf("所有学生的平均分数: %.2f\n", (float)total / (student_count * MAX_COURSES));
}

// 按课程统计学生分数
void query_by_course() {
    int course_index;
    printf("请输入课程编号（1-6）: ");
    scanf("%d", &course_index);

    if (course_index < 1 || course_index > MAX_COURSES) {
        printf("无效的课程编号。\n");
        return;
    }

    int total = 0;
    for (int i = 0; i < student_count; i++) {
        total += students[i].scores[course_index - 1];
    }

    printf("课程 %d 的平均分数: %.2f\n", course_index, (float)total / student_count);
}

// 查询某课程的最高分学生信息
void query_highest_score_by_course() {
    int course_index;
    printf("请输入课程编号（1-6）: ");
    scanf("%d", &course_index);

    if (course_index < 1 || course_index > MAX_COURSES) {
        printf("无效的课程编号。\n");
        return;
    }

    int max_score = -1;
    int index = -1;
    for (int i = 0; i < student_count; i++) {
        if (students[i].scores[course_index - 1] > max_score) {
            max_score = students[i].scores[course_index - 1];
            index = i;
        }
    }

    if (index != -1) {
        printf("课程 %d 的最高分: %d，学生: %s（班级: %s）\n", 
               course_index, max_score, students[index].name, students[index].class);
    } else {
        printf("未找到学生。\n");
    }
}

// 显示菜单
void display_menu() {
    printf("\n学生信息管理系统\n");
    printf("1. 添加学生\n");
    printf("2. 删除学生\n");
    printf("3. 修改学生\n");
    printf("4. 查询学生人数\n");
    printf("5. 按班级查询学生人数\n");
    printf("6. 计算学生平均分数\n");
    printf("7. 按课程统计学生分数\n");
    printf("8. 查询某课程最高分学生信息\n");
    printf("9. 退出\n");
}

int main() {
    load_students();

    int choice;
    do {
        display_menu();
        printf("请输入您的选择: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_student();
                break;
            case 2:
                delete_student();
                break;
            case 3:
                modify_student();
                break;
            case 4:
                query_student_count();
                break;
            case 5:
                query_by_class();
                break;
            case 6:
                calculate_average_score();
                break;
            case 7:
                query_by_course();
                break;
            case 8:
                query_highest_score_by_course();
                break;
            case 9:
                printf("正在退出...\n");
                break;
            default:
                printf("无效的选择，请重试。\n");
        }
    } while (choice != 9);

    return 0;
}